package com.yiliao.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiliao.service.AlipaySetUpServvice;
import com.yiliao.util.DateUtils;
import com.yiliao.util.MessageUtil;
import com.yiliao.util.RedisUtil;

import net.sf.json.JSONObject;

@Service("alipaySetUpServvice")
public class AlipaySetUpServiceImpl extends ICommServiceImpl implements AlipaySetUpServvice {

	@Autowired
	RedisUtil redisUtil;

	@Override
	public MessageUtil setAlipaySetUp(Integer t_id, String t_alipay_appid, String t_alipay_private_key,
			String t_alipay_public_key, int t_pay_id) {
		try {
			// 新增
			if (t_id == null || t_id <= 0) {
				
				List<Map<String,Object>> sqltoMap = this.getFinalDao().getIEntitySQLDAO().findBySQLTOMap("SELECT t_alipay_public_key FROM t_alipay_setup WHERE t_pay_id =?", t_pay_id);
				
				if(null != sqltoMap && !sqltoMap.isEmpty()) {
					//取得公钥
					long count = sqltoMap.stream().filter(s -> !t_alipay_public_key.equals(s.get("t_alipay_public_key").toString())).count();
					
					if(count > 0) {
						return new MessageUtil(-1, "同一个支付配置不能配置多个不同的支付key!");
					}
				}
				
				String inSql = "INSERT INTO t_alipay_setup (t_alipay_appid, t_alipay_public_key, t_alipay_private_key,t_pay_id,t_create_time) VALUES (?, ?, ?, ?, ?);";
				this.getFinalDao().getIEntitySQLDAO().executeSQL(inSql, t_alipay_appid.trim(),
						t_alipay_public_key.trim(), t_alipay_private_key.trim(), t_pay_id,
						DateUtils.format(new Date(), DateUtils.FullDatePattern));
			} else { // 修改

				String uSql = "UPDATE t_alipay_setup SET t_alipay_appid=?, t_alipay_public_key=?, t_alipay_private_key=?,t_pay_id = ? , t_create_time=? WHERE t_id=?; ";
				this.getFinalDao().getIEntitySQLDAO().executeSQL(uSql, t_alipay_appid, t_alipay_public_key,
						t_alipay_private_key, t_pay_id, DateUtils.format(new Date(), DateUtils.FullDatePattern), t_id);
			}
			// 覆盖redis中的数据
			redisUtil.hmset("pay_deploy_" + t_pay_id, new HashMap<String, String>() {
				private static final long serialVersionUID = 1L;
				{
					put("appId", t_alipay_appid);
					put("publicKey", t_alipay_public_key);
					put("privateKey", t_alipay_private_key);
				}
			});

			return new MessageUtil(1, "更新成功!");

		} catch (Exception e) {
			e.printStackTrace();
			return new MessageUtil(0, "程序异常!");
		}
	}

	/*
	 * 删除支付宝设置 (non-Javadoc)
	 * 
	 * @see com.yiliao.service.AlipaySetUpServvice#delAlipaySetUp(int)
	 */
	@Override
	public MessageUtil delAlipaySetUp(int t_id) {
		try {

			// 获取配置编号
			Map<String, Object> toMap = this.getFinalDao().getIEntitySQLDAO()
					.findBySQLUniqueResultToMap("SELECT t_pay_id FROM t_alipay_setup WHERE t_id = ?", t_id);

			//清理缓存中的配置
			if(null != toMap && !toMap.isEmpty()) {
				redisUtil.remove("pay_deploy_" + toMap.get("t_pay_id"));
			}
			
			String dSql = " DELETE FROM t_alipay_setup WHERE t_id = ? ";

			this.getFinalDao().getIEntitySQLDAO().executeSQL(dSql, t_id);

			return new MessageUtil(1, "删除成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new MessageUtil(0, "程序异常!");
		}
	}

	@Override
	public JSONObject getAlipaySetUpList(int page) {
		try {

			String qSql = " SELECT al.t_id,al.t_alipay_appid,al.t_alipay_private_key,al.t_alipay_public_key,DATE_FORMAT(al.t_create_time,'%y-%m-%d %T') AS t_create_time,al.t_pay_id,p.t_pay_name FROM t_alipay_setup al LEFT JOIN t_pay_dispose p ON al.t_pay_id = p.t_id  ";

			List<Map<String, Object>> sqltoMap = this.getFinalDao().getIEntitySQLDAO().findBySQLTOMap(qSql);

			return JSONObject.fromObject(new HashMap<String, Object>() {
				private static final long serialVersionUID = 1L;
				{
					put("total", sqltoMap.isEmpty() ? 0 : 1);
					put("rows", sqltoMap);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取支付宝配置列表
	 */
	@Override
	public MessageUtil getAlipayDeployList() {
		try {

			List<Map<String, Object>> sqltoMap = this.getFinalDao().getIEntitySQLDAO().findBySQLTOMap(
					"SELECT t_id,t_pay_name FROM t_pay_dispose WHERE t_pay_type not in ( -2,-5) AND t_is_scrap = 1");
			return new MessageUtil(1, sqltoMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
